import 'package:flutter/material.dart';


// 卡片1
class Cardview1 extends StatefulWidget {

  final String title; // 标题
  final String content; // 内容
  final dynamic children; // 内容
  final dynamic buttonList; // 自定义 按钮
  final dynamic defaultBtnConfig; // 默认按钮

  Cardview1({
    Key key,
    this.title,
    this.content,
    this.defaultBtnConfig,
    this.children,
    this.buttonList,

  }) : super(key: key);  

  @override
  _Cardview1State createState() => new _Cardview1State();
}

class _Cardview1State extends State<Cardview1> {


    // 内容显示 循环  
    Widget _contentList() {

        if( widget.children != null ){

          List<Widget> list = [];

          for(var item in widget.children ) {
            list.add(
              Container(
                padding: EdgeInsets.fromLTRB(8,8,8,8),
                decoration: BoxDecoration(
                  border:Border(
                    bottom: BorderSide(
                      width: 1,
                      color: Color(0xffe5e5e5)
                    )
                  )
                ),                
                child: Row(
                  children: <Widget>[
                    Expanded(
                      flex: 3,
                      child: Padding(
                        padding: EdgeInsets.fromLTRB(0,0,26,0),
                        child: Text(item["label"].toString()),
                      )
                    ),
                    Expanded(
                      flex: 3,
                      child: Text(item["value"].toString())
                    )
                  ],
                )
              )
            );
          }

          return Column(
              children: list.toList(), 
          );
        }


      return null;        

    }

    // 自定义 按钮  
    Widget _customBtnInit() {

      if( widget.buttonList != null ){
        List<Widget> _list = [];

        // Widget content; 
        for(var item in widget.buttonList ) {
          _list.add(
            Container(
              margin: EdgeInsets.fromLTRB(0,2,0,2),
              padding: EdgeInsets.fromLTRB(10,0,0,0),
              height: 28,
              // alignment: Alignment.topRight,
              child: RaisedButton(              
                textTheme: ButtonTextTheme.accent,
                color: item["icon"]!=null?item["icon"]:Colors.blue[500],
                padding: EdgeInsets.fromLTRB(8,2,8,2),
                onPressed: () {
                  if( item["onClick"]!=null ){
                    item["onClick"](item);
                  }
                },
                child: Text(item["label"]!=null?item["label"].toString():"",style: TextStyle(color: Colors.white, fontSize: 14),
                ),
              )
            )
          );
        }

        return Wrap(
          children: _list.toList(), 
        );
      }

      return null;
    }


    @override
    Widget build(BuildContext context) {
      return  Container(
          margin: EdgeInsets.fromLTRB(8,8,8,8),
          padding: EdgeInsets.fromLTRB(8,8,8,8),
          decoration: BoxDecoration(
              color: Colors.white,
              border: Border.all(width:1,color:Colors.black12),
              borderRadius: BorderRadius.all(Radius.circular(6.0)),
          ),
          child: Row(
            children: [
                Expanded(
                  flex: 11,
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [

                      Container(
                        // padding: EdgeInsets.fromLTRB(0,8,0,8),
                        // color: Colors.blue,
                        child: ( (widget.title!=null) || (widget.defaultBtnConfig!=null) )?
                          Row(
                            children: <Widget>[
                              Expanded(
                                flex: 11,
                                child: Padding(
                                  padding: EdgeInsets.fromLTRB(0,2,28,12),
                                  child: Text(
                                    widget.title!=null?widget.title.toString():"",
                                    overflow: TextOverflow.ellipsis,
                                    style: TextStyle(
                                      fontWeight: FontWeight.bold,
                                    ),
                                  )
                                )
                              ),
                              Expanded(
                                flex: 6,
                                child: InkWell(
                                  onTap: (){
                                    if( widget.defaultBtnConfig!=null && widget.defaultBtnConfig["onClick"]!=null ){
                                      widget.defaultBtnConfig["onClick"](widget.defaultBtnConfig);
                                    }
                                  },
                                  child: (widget.defaultBtnConfig!=null)?
                                    Row(
                                      children: <Widget>[
                                        Expanded(
                                          flex: 5,
                                          child: Text(
                                            (widget.defaultBtnConfig["text"]!=null?widget.defaultBtnConfig["text"].toString():" "),
                                            textAlign: TextAlign.right,
                                            overflow: TextOverflow.ellipsis,
                                            style: TextStyle(
                                              color: (widget.defaultBtnConfig["color"]!=null?widget.defaultBtnConfig["color"]:Colors.black87)
                                            ),
                                          )
                                        ),
                                        Expanded(
                                          flex: 2,
                                          child: widget.defaultBtnConfig["icon"]!=null ? Icon(
                                            widget.defaultBtnConfig["icon"],
                                            color: Color.fromARGB(255, 126, 126, 126), 
                                          ):Text('')
                                        )
                                      ],
                                    )
                                    :Text('')
                                ) 
                              )
                            ],
                          )
                          :null
                      ),

                      Container(
                        padding: EdgeInsets.fromLTRB(8,0,8,0),
                        child: widget.content!=null ?
                        Text(
                          widget.content.toString(),
                          // overflow: TextOverflow.ellipsis,
                          style: TextStyle(
                            // color: Colors.grey[500],
                          ),
                        )
                        :null
                      ),

                      Container(
                        child: widget.children!=null ?_contentList():null                                        
                      ),

                      Container(
                        padding: EdgeInsets.fromLTRB(0,6,0,0),
                        alignment: Alignment.topRight,
                        child: widget.buttonList!=null? _customBtnInit():null
                      )

                      
                    ],
                  ),
                )
            ],
          )
      );
    }  
}